package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.Exception.MyException;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author: Eric
 * @since: 2020/11/28
 */
@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        try {
            member.setRegTime(new Date());
            member.setBirthday(DateUtils.parseString2Date(member.getDate()));
            memberDao.add(member);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 调用服务查询到每个最后一天为止的会员总数量
     *
     * @param months
     * @return
     */
    @Override
    public List<Integer> getMemberReport(List<String> months) {
        List<Integer> memberCount = new ArrayList<Integer>(months.size());
        for (String month : months) {
            // 2020-11
            // 拼接最后一天  2020-11-31
            month += "-31";
            Integer count = memberDao.findMemberCountBeforeDate(month);
            memberCount.add(count);
        }
        return memberCount;
    }

    /**
     * 使用分页插件，调用服务查询会员列表
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult<Member> findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        if (!StringUtils.isEmpty(queryPageBean.getQueryString())) {
            queryPageBean.setQueryString("%" + queryPageBean.getQueryString() + "%");
        }
        Page<Member> memberPage = memberDao.selectByCondition(queryPageBean.getQueryString());

        return new PageResult<Member>(memberPage.getTotal(), memberPage.getResult());
    }

    @Override
    public Member findById(int id) {
        return memberDao.findById(id);
    }

    /**
     * 删除会员
     * @param id
     * @throws MyException
     */
    @Override
    public void deleteById(int id) throws MyException {
        int count = memberDao.findOrderMember(id);
        if (count != 0 && count > 0) {
            throw new MyException("该会员有相关的预约未执行，无法删除 ");
        }
        memberDao.deleteById(id);
    }

    /**
     * 修改会员
     * @param member
     */
    @Override
    public void update(Member member) {
        memberDao.update(member);
    }

    /**
     * 获取男女比例
     * @return
     */
    @Override
    public List<Map<String, Object>> getGender() {

        return memberDao.getSex();
    }

    /**
     * 获取年龄段占比
     * @return
     */
    @Override
    public List<Map<String, Object>> getAge() {

        return memberDao.getAge();
    }
}

